/*
THIS FILE COMPUTES THE EXPECTED EXCESS RETURN USING  THE PAST SHARES INSTEAD OF THE AVERAGE SHARES 	TO WEIGHT OTHER COUNTRIES EXCESS RETURNS
*/

use "$datapath/Portfolio_Paper_zbar.dta", clear
* us differentials
merge m:1  datem country  using "$datapath/Portfolio_Paper_Predictions.dta"
replace er_24d94 = 0 if country == "unitedstates"
drop if mi(fundid)
drop _merge

keep  datem fundid country lz_*  er_24d94
*****************************************************************************

*******************************************************************************
*** fund weighted us differential
levelsof country, local(country)
foreach c of local country {
	qui gen sum_er_24d94_`c' = lz_`c'*er_24d94
}
*****************************************************************************

*****************************************************************************
* reduce information and reshape the data
qui collapse (sum) sum_* , by(fundid datem)

foreach v of varlist sum_* {
	qui replace `v' = . if `v' == 0
}

qui reshape long sum_er_24d94_ , i(datem fundid) j(country) string
drop if mi(sum_er_24d94_)
*****************************************************************************

*****************************************************************************
* DIFFERENTIALS

* import us differentials
merge m:1  datem country  using "$datapath/Portfolio_Paper_Predictions.dta"
replace er_24d94 = 0 if country == "unitedstates"
drop if mi(fundid)
drop _merge

* differential
qui gen zint_er_24d94 = er_24d94 - sum_er_24d94_

keep datem fundid country zint_*

save "$datapath/Portfolio_Paper_Excessreturn_Weighting_Table_B1_Col_6.dta", replace
* end
*****************************************************************************